package cn.northsea.controller;

import cn.northsea.entity.PrivateChatMessage;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author 郑智辰~
 * @version 1.0
 * 序列化工具
 */
@Component
public class SerializeUtil {

    // 将 List 序列化为 JSON 字符串
    public String serializeList(List<PrivateChatMessage> messagesList) throws JsonProcessingException {
        String jsonString = JSON.toJSONString(messagesList);
        return jsonString;
    }

    // 将 JSON 字符串反序列化为 List
    public List<PrivateChatMessage> deserializeList(String json) throws IOException {
        List<PrivateChatMessage> privateChatMessages = JSON.parseObject(json, new TypeReference<List<PrivateChatMessage>>() {
        });
        return privateChatMessages;
    }
}
